package com.shwread.android.ui.dialog;


import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.shwread.android.qysw.activity.MainActivity;
import com.shwread.android.qysw10000204.R;
import com.shwread.android.ui.widget.datepicker.NumericWheelAdapter;
import com.shwread.android.ui.widget.datepicker.OnWheelChangedListener;
import com.shwread.android.ui.widget.datepicker.WheelView;
import com.shwread.httpsdk.util.Util;

import logic.constants.DefaultConsts;
import logic.util.DateUtil;


public class DatePickerDialog
        extends
        Dialog
{
	private Activity activity;
	private String value;
	private DatePickerListener listener;
	private int minValue = 1900;
	private int maxValue = 2050;
	private TextView cancel,ok;
	private WheelView yearPicker,monthPicker,dayPicker;
	
	public DatePickerDialog(Activity activity, String value, DatePickerListener listener) {
		super(activity, R.style.common_dialog);
		this.activity = activity;
		this.value=value;
		this.listener = listener;
	}
    @Override
	protected void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setCanceledOnTouchOutside(true);
	    initView();
	    initData();
	    getWindow().setLayout(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
	    getWindow().setGravity(Gravity.BOTTOM);
	}

	private void initView() {
		final View view = LayoutInflater.from(activity).inflate(R.layout.datetime_dialog, null);
		yearPicker = (WheelView) view.findViewById(R.id.datetime_picker_year);
		monthPicker = (WheelView) view.findViewById(R.id.datetime_picker_month);
		dayPicker = (WheelView) view.findViewById(R.id.datetime_picker_day);
		cancel = (TextView) view.findViewById(R.id.datetime_cancel);
		ok = (TextView) view.findViewById(R.id.datetime_ok);
		setContentView(view);
		
		ClickListener l = new ClickListener();
		ok.setOnClickListener(l);
		cancel.setOnClickListener(l);
    }
	
	private class ClickListener implements View.OnClickListener {
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.datetime_ok:
				clickOk();
				break;
			case R.id.datetime_cancel:
				clickCancel();
				break;
			}
		}
	}
	private void clickCancel() {
		dismiss();
	}
	private void clickOk() {
		listener.onSelect(getValue());
		if(activity instanceof MainActivity){
			((MainActivity)activity).getUIHandler().sendEmptyMessage(DefaultConsts.PERSONAL_UPDATEUI_USERINFO_BIRTHDAY);
		}
		dismiss();
	}
	private String getValue(){
		StringBuffer sb = new StringBuffer();
		sb.append(yearPicker.getCurrentItem()+minValue);
		sb.append("-");
		sb.append(format(monthPicker.getCurrentItem()+1));
		sb.append("-");
		sb.append(format(dayPicker.getCurrentItem()+1));
		System.out.println("DatePickerDialog --> "+sb.toString());
		return sb.toString();
	}
    private String format(int value){
		return String.format("%02d", value);
	}
	private void initData() {
		int curYear;
		int curMonth;
		int curDay;
		if(Util.isNotEmpty(value)) {
			String[] strings = value.split("-");
			curYear = Integer.parseInt(strings[0]);
			curMonth = Integer.parseInt(strings[1])-1;
			curDay = Integer.parseInt(strings[2]);
		} else {
			curYear = DateUtil.getYear();
			curMonth = DateUtil.getMonth()-1;
			curDay = DateUtil.getDay();
		}
		System.out.println("curYear:"+curYear);
		System.out.println("curMonth:"+curMonth);
		System.out.println("curDay:"+curDay); 
		
		yearPicker.setAdapter(new NumericWheelAdapter(minValue, maxValue));
		yearPicker.setLabel("年");
		yearPicker.setCyclic(true);
		yearPicker.setCurrentItem(curYear-minValue);
		
		monthPicker.setAdapter(new NumericWheelAdapter(1, 12,"%02d"));
		monthPicker.setLabel("月");
		monthPicker.setCyclic(true);
		monthPicker.setCurrentItem(curMonth);
		
		dayPicker.setAdapter(new NumericWheelAdapter(1, DateUtil.getMaxDayByMonth(curYear, curMonth),"%02d"));
		dayPicker.setLabel("日");
		dayPicker.setCyclic(true);
		dayPicker.setCurrentItem(curDay-1);
		
		OnWheelChangedListener wheelListener = new OnWheelChangedListener() {
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				int v = DateUtil.getMaxDayByMonth(yearPicker.getCurrentItem(), monthPicker.getCurrentItem());
				dayPicker.setAdapter(new NumericWheelAdapter(1, v,"%02d"));
				dayPicker.setLabel("日");
				dayPicker.setCurrentItem(0);
			}
		};
		yearPicker.addChangingListener(wheelListener);
		monthPicker.addChangingListener(wheelListener);
		
    }
	
}
